<?php
class SofaUser
{
	private $freecomments = 600;
	private $increment = 23;
	public function ShowFirstLine()
	{
		$result = mysql_query("SELECT * FROM sofauser",$this->m_db);
		printf("User ID: %s<br>\n", mysql_result($result,0,"id"));
		printf("Last Visit: %s<br>\n", mysql_result($result,0,"lastlogin"));
	}
	private $m_db; 
    public function __construct() 
	{ 
        $this->m_db = mysql_connect("localhost", "y182319", "hao123"); 
		mysql_select_db("y182319",$this->m_db);
    }
	public function Insert($cpuid)
	{
		$insertCmd = "INSERT INTO sofauser values('" .$cpuid."',NOW(),NOW(),".$this->freecomments.",'')";
		$result = mysql_query($insertCmd,$this->m_db);
		return $result;
	}
	public function Query($cpuid)
	{
		$selectCmd = "SELECT * FROM sofauser where id like '".$cpuid."'";
		$result = mysql_query($selectCmd,$this->m_db);
		$rs = mysql_fetch_array($result);
		return $rs;
	}
	public function UpdateDb($cpuid,$commentsleft)
	{
		$updateCmd = "UPDATE sofauser SET commentsleft=".$commentsleft.",lastlogin=NOW() WHERE id like '".$cpuid."'";
		$result = mysql_query($updateCmd,$this->m_db);
		return $result ;
	}
	public function GetSofaInfo($cpuid)
	{
		$rs = $this->Query($cpuid);
		if( !$rs )
		{
			$this->Insert($cpuid);
			return $this->freecomments;
		}
		$this->UpdateDb($cpuid,$rs["commentsleft"]-$this->increment);
		return $rs["commentsleft"]-$this->increment;
	}
}
?>